package com.chris.conference;

import com.chris.conference.Class.Record;
import com.chris.conference.Class.Room;
import com.chris.conference.Class.SimpleRecord;
import com.chris.conference.Class.User;
import com.chris.conference.Repository.RecordRepository;
import com.chris.conference.Repository.RoomRepository;
import com.chris.conference.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;

@RestController
public class Controller {

    @Autowired
    private RecordRepository recordRepository;
    @Autowired
    private RoomRepository roomRepository;
    @Autowired
    private UserRepository userRepository;

    //查询所有记录
    @GetMapping(value = "/records")
    public List<Record> recordFindAll(){
        return recordRepository.findAll();
    }

    //根据id查询记录
    @GetMapping(value = "/record/id/{id}")
    public Record recordFindById(@PathVariable("id") Integer id){
        return recordRepository.findById(id).get();
    }

    //根据用户id查询记录
    @GetMapping(value = "/record/userid/{userid}")
    public List<Record> recordFindByUserId(@PathVariable("userid") String userid){
        return recordRepository.findByUserid(userid);
    }

    //根据房间id查询记录
    @GetMapping(value = "/record/roomid/{roomid}")
    public List<Record> recordFindByRoomId(@PathVariable("roomid") String roomid){
        return recordRepository.findByRoomid(roomid);
    }

    //根据状态查询记录
    @GetMapping(value = "/record/status/{status}")
    public List<Record> recordFindByStatus(@PathVariable("status") String status){
        return recordRepository.findByStatus(status);
    }

    //根据房间id查询一周的记录
    @PostMapping(value = "/record/week")
    public List<SimpleRecord> recordFindByWeek(@RequestParam("roomid") String roomid,
                                               @RequestParam("date") String date){
        SimpleRecordSearch search = new SimpleRecordSearch(userRepository);
        return search.simpleRecordFindByWeek(recordRepository.findByWeek(date,roomid));
    }

    //根据时间段查询、用户、房间查询记录
    @PostMapping(value = "/record/time_userid_roomid")
    public List<Record> recordFindByTime_Userid_Roomid(@RequestParam("startdate") String startdate,
                                                       @RequestParam("enddate")String enddate,
                                                       @RequestParam("userid")String userid,
                                                       @RequestParam("roomid") String roomid,
                                                       @RequestParam("status") String status){
        if(userRepository.findByUserid(userid).getRank() == 1){
            if(roomid.equals("0")){
                return recordRepository.findByDate(startdate,enddate,status);
            }
            else {
                return recordRepository.findByDate_Roomid(startdate,enddate,roomid,status);
            }
        }
        else {
            if(roomid.equals("0")){
                return recordRepository.findByDate_Userid(startdate,enddate,userid,status);
            }
            else {
                return recordRepository.findByDate_Userid_Roomid(startdate,enddate,userid,roomid,status);
            }
        }
    }

    @PostMapping(value = "/record")
    public String recordAdd(@RequestParam("userid")String userid,
                            @RequestParam("roomid")String roomid,
                            @RequestParam("starttime")Timestamp starttime,
                            @RequestParam("endtime")Timestamp endtime,
                            @RequestParam("createdtime")Timestamp createdtime,
                            @RequestParam("topic")String topic){
        if(recordRepository.findByTime(starttime,endtime,roomid).isEmpty()){
            Record record = new Record(roomid,userid,starttime,endtime,createdtime,topic,"0");
            recordRepository.save(record);
            return "success";
        }
        else {
            return "exist";
        }
    }

    //申请记录审核
    @PostMapping(value = "record/check")
    public Record recordCheck(@RequestParam("id")Integer id,
                              @RequestParam("status")String status){
        Record record = recordRepository.findById(id).get();
        record.setStatus(status);
        return recordRepository.save(record);
    }

    //根据id删除记录
    @GetMapping(value = "record/del/{id}")
    public String recordDelete(@PathVariable("id") Integer id){
        if(recordRepository.findById(id).isPresent()) {
            recordRepository.deleteById(id);
            return "success";
        }
        else {
            return "none";
        }
    }

    //查询所有房间
    @GetMapping(value = "/rooms")
    public List<Room> roomFindAll(){
        return roomRepository.findAll();
    }

    //根据房间名称模糊查询房间
    @GetMapping(value = "/roomlike/{name}")
        public List<Room> roomFindAllByNameLike(@PathVariable("name")String name){
        return roomRepository.findAllByNameLike("%" + name +"%");
    }

    @PostMapping(value = "/room")
    public Room roomAdd(@RequestParam("name") String name){
        Room room = new Room(name);
        return roomRepository.save(room);
    }

    //根据id删除房间
    @GetMapping(value = "/room/del/{id}")
    public String roomDelete(@PathVariable("id") Integer id){
        if(roomRepository.findById(id).isPresent()){
            roomRepository.deleteById(id);
            return "success";
        }
        else {
            return "none";
        }
    }

    //查询所有用户
    @GetMapping(value = "/users")
    public List<User> userFindAll(){
        return userRepository.findAll();
    }

    //根据用户id查询用户
    @GetMapping(value = "/user/userid/{userid}")
    public User userFindByUserId(@PathVariable("userid") String userId){
        return userRepository.findByUserid(userId);
    }

    //根据昵称查询用户
    @GetMapping(value = "/user/nickname/{nickname}")
    public User userFindByNickname(@PathVariable("nickname") String nickname){
        return userRepository.findByNickname(nickname);
    }

    @PostMapping(value = "/user")
    public User userAdd(@RequestParam("userid")String userid,
                        @RequestParam("username") String username,
                        @RequestParam("nickname") String nickname,
                        @RequestParam("phone") String phone,
                        @RequestParam("rank") Integer rank){
        User user = new User(userid,username,nickname,phone,rank);
        return userRepository.save(user);
    }

    //根据id删除用户
    @GetMapping(value = "user/del/{id}")
    public String userDelete(@PathVariable("id") Integer id){
        if (userRepository.findById(id).isPresent()){
            userRepository.deleteById(id);
            return "success";
        }
        else {
            return "none";
        }
    }
}
